# -*- coding:utf-8 -*-

# @Time : 2023/10/5 10:12
# @Author : 快乐的小猴子
# @Version : 
# @Function : 

import time
import requests
import base64
import json
from hashlib import md5
from Crypto.Cipher import AES
from Crypto.Util.Padding import unpad, pad

word = input('请输入要翻译的单词：')
obj = md5()
d = 'fanyideskweb'
e = int(time.time() * 1000)
# print(e)
u = 'webfanyi'
t = 'fsdsogkndfokasodnaso'
string = f"client={d}&mysticTime={e}&product={u}&key={t}"
# print(string)
obj.update(string.encode('utf-8'))
sign = obj.hexdigest()
# print(sign)
data = {
    "i": word,
    "from": "auto",
    "to": "",
    "dictResult": "true",
    "keyid": "webfanyi",
    "sign": sign,
    "client": "fanyideskweb",
    "product": "webfanyi",
    "appVersion": "1.0.0",
    "vendor": "web",
    "pointParam": "client,mysticTime,product",
    "mysticTime": e,
    "keyfrom": "fanyi.web",
    "mid": 1,
    "screen": 1,
    "model": 1,
    "network": "wifi",
    "abtest": 0,
    "yduuid": "abcdefg",
}
headers = {
    "Accept": "application/json, text/plain, */*",
    "Accept-Encoding": "gzip, deflate, br",
    "Accept-Language": "zh-CN,zh;q=0.9",
    "Cache-Control": "no-cache",
    "Connection": "keep-alive",
    "Content-Type": "application/x-www-form-urlencoded",
    "Cookie": "OUTFOX_SEARCH_USER_ID_NCOO=811595811.0360494; OUTFOX_SEARCH_USER_ID=1193880793@2408:8207:3013:9db0:d4ec:d8a:4e13:99d5",
    "Host": "dict.youdao.com",
    "Origin": "https://fanyi.youdao.com",
    "Pragma": "no-cache",
    "Referer": "https://fanyi.youdao.com/",
    "Sec-Fetch-Dest": "empty",
    "Sec-Fetch-Mode": "cors",
    "Sec-Fetch-Site": "same-site",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36",
    "sec-ch-ua": "\"Google Chrome\";v=\"117\", \"Not;A=Brand\";v=\"8\", \"Chromium\";v=\"117\"",
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": "\"Windows\"",
}
url = 'https://dict.youdao.com/webtranslate'
resp = requests.post(url=url, headers=headers, data=data)
# print(resp.text)
keyobj = md5()
keyobj.update("ydsecret://query/key/B*RGygVywfNBwpmBaZg*WT7SIOUP2T0C9WHMZN39j^DAdaZhAnxvGcCY6VYFwnHl".encode('utf-8'))
key = keyobj.digest()
# print(len(key))
ivobj = md5()
ivobj.update('ydsecret://query/iv/C@lZe2YzHtZ2CYgaXKSVfsb7Y4QWHjITPPZ0nQp87fBeJ!Iv6v^6fvi2WN@bYpJ4'.encode('utf-8'))
iv = ivobj.digest()
ciphertext = base64.b64decode(resp.text.replace('_', '/').replace('-', '+'))
aes = AES.new(key=key, mode=AES.MODE_CBC, iv=iv)
result = unpad(aes.decrypt(ciphertext), 16)
# print(result.decode())
all_trans = json.loads(result.decode())["dictResult"]["ec"]['word']

if 'trs' in list(all_trans.keys()):
    word1 = json.loads(result.decode())["dictResult"]["ec"]['word']["trs"]
    for word in word1:
        pos = word['pos']
        tran = word['tran']
        print(pos, tran)
if 'wfs' in list(all_trans.keys()):
    word2 = json.loads(result.decode())["dictResult"]["ec"]['word']['wfs']
    wf_tran = ''
    for word in word2:
        wfs_name = wf_tran + word['wf']['name'] + ': ' + word['wf']['value']
        print(wfs_name)



